<?php
class reportParamatersForm extends BaseForm
{
	
  var $userFunction;
  
  function setUserFunction($userFunction) {
    $this->userFunction = $userFunction;
  }
  function callUserFunction($reportId = null) {
    $func = $this->userFunction;
    return $func($this, $reportId);
  }
  
  public function configure()
  {
  	parent::configure();
  	$formaterName = sfConfig::get('app_form_formatterName','app');
	$this->getWidgetSchema()->setFormFormatterName($formaterName);
  }
  
  public function configureFromXML($doc)
  {
  	//parent::configure();
  	$conn = Doctrine_Manager::getInstance()->getConnection('doctrine');
  	$paramsDef = $doc->getElementsByTagName("parametres") ;
  	if($paramsDef){
  		if($paramsDef->item($k)){
		  	$params = $paramsDef->item($k)->getElementsByTagName("parametre") ;
		    foreach($params as $param){
		    	$this->setMyWidget($param, $conn);
			}  	
  		}
  	}
  }
  
  public function setMyWidget($param, $conn = null,$options = null){
  	if(is_null($conn)) $conn = Doctrine_Manager::getInstance()->getConnection('doctrine');
  	
  	$i18N = sfContext::getInstance()->getI18N();
  	
  	$name = $param->getAttribute('name');
	$type = $param->getElementsByTagName('type')->item($k)->nodeValue;
	$label = $param->getElementsByTagName('label')->item($k)->nodeValue;
	if($param->getElementsByTagName('defaultValue'))
		$defaultValue = $param->getElementsByTagName('defaultValue')->item($k)->nodeValue;
	if($type=='BO'){
		if(is_null($options)){
			$sqlText = $param->getElementsByTagName('sqlText')->item($k)->nodeValue;
			$fieldToShow = $param->getElementsByTagName('fieldToShow')->item($k)->nodeValue;
			$fieldToAssign = $param->getElementsByTagName('fieldToAssign')->item($k)->nodeValue;
			
			$options['']=sfContext::getInstance()->getI18N()->__('Please select a value...');
			$results = $conn->execute($sqlText)->fetchAll();
			foreach($results as $item){
				$key = $item[$fieldToAssign];
				$value = $item[$fieldToShow];
				$options[$key]=$value;	
			}
		}
		$this->widgetSchema[$name] = new sfWidgetFormChoice(
										array('multiple' => false,
				  					    'expanded' => false,
										'choices'  => $options,
										));
 	}elseif($type == 'enum'){
		if(!isset($options)){
			$enumValues = $param->getElementsByTagName('enumValues')->item($k)->nodeValue;
			$options = Util::getArrayFromStr(Util::stripAccents2($enumValues),';',true,'=>');
		}
		$this->widgetSchema[$name] = new sfWidgetFormChoice(
		array('choices'=>$options, 'multiple' => false,'expanded' => false));
	}elseif($type=='date'){
	     $this->widgetSchema[$name] = new sfWidgetFormDateJS();
	}elseif($type=='boolean'){
		 $this->widgetSchema[$name] = new sfWidgetFormInputCheckbox();
	}elseif($type=='array'){
		if(is_null($options)){
			$sqlText = $param->getElementsByTagName('sqlText')->item($k)->nodeValue;
			$fieldToShow = $param->getElementsByTagName('fieldToShow')->item($k)->nodeValue;
			$fieldToAssign = $param->getElementsByTagName('fieldToAssign')->item($k)->nodeValue;
			
			$options['']=sfContext::getInstance()->getI18N()->__('You can select multiple choices (Ctrl + clic)');
			$results = $conn->execute($sqlText)->fetchAll();
			foreach($results as $item){
				$key = $item[$fieldToAssign];
				$value = $item[$fieldToShow];
				$options[$key]=$value;	
			}
		}
		$this->widgetSchema[$name] = new sfWidgetFormChoice(
				array('choices'=>$options, 'multiple' => true,'expanded' => false));				
	}else{
		 $this->widgetSchema[$name] = new sfWidgetFormInputText();
	}

	$this->widgetSchema[$name]->setLabel($label);
	
	/****		 
	 * * HELPS
	 * *****************/
	
	$helps = $this->widgetSchema->getHelps();
	if($param->getElementsByTagName('help')){
		$help = $param->getElementsByTagName('help')->item($k)->nodeValue;
		$helps[$name] = $i18N->__($help,null,'forms');
		$this->widgetSchema->setHelps($helps);
		unset($helps);	
	}
	/****
	 * HTML ATTRIBUTES
	 * (Including tmt validator !)
	 * *****************/
	$attributes=array('class' => 'text');
	$attributes['tmt:message'] = '';
	$concatError = '';
	if($type=='BO'){
		$attributes['class'] = 'combobox';
	}elseif($type=='integer')
		{
			$attributes['tmt:pattern'] = "integer";
			$attributes['tmt:message'] = $concatError . $i18N->__("Incorrect Integer");
			$concatError = ' / ';
		}
	elseif($type=='float')
		{
			$attributes['tmt:pattern'] = "number";
			$attributes['tmt:message'] .= $concatError . $i18N->__("Incorrect Float");
			$concatError = ' / ';
		}
	elseif($type=='date')
		{
			$attributes['class'] = 'date';
			$attributes['tmt:datepattern'] = "DD/MM/YYYY";
			if(!isset($attributes['tmt:message']))
				$attributes['tmt:message'] = '';
			$attributes['tmt:message'] .= $concatError .  $i18N->__("Incorrect date format"); //Formato de fecha incorrecto "Le format de la date est incorrecte";
			$concatError = ' / ';
		}
	elseif($type=='boolean')
		{
			$attributes['class'] = 'radioCheck';
		}
	$notnull = false;	
  	if($param->getElementsByTagName('notnull')){
		$notnull = $param->getElementsByTagName('notnull')->item($k)->nodeValue;
	}
	if($notnull)
		{	
			$attributes['class'] = $attributes['class'].' required';
			$attributes['tmt:required'] = "true";
			$attributes['tmt:errorclass']=  "invalid";
			if(!isset($attributes['tmt:message']))
				$attributes['tmt:message'] = '';
				
			$attributes['tmt:message'] .= $i18N->__("Required Attribute"); //"Atributo Obligatorio"; "Champ obligatoire";
			$concatError = ' / ';			
			if($type=='BO')
			{
				$attributes['tmt:invalidindex'] = "0";
			}
		}
	$this->offsetGet($name)->getWidget()->setAttributes($attributes);	
  	if($param->getElementsByTagName('defaultValue')){
  		$default = $param->getElementsByTagName('defaultValue')->item($k)->nodeValue;
		$default = Util::processDefaultExpression($default, $type, "d/m/Y");
	    $this->setDefault($name, $default);
  		
	    /*if($type=='boolean'){
			if($default=='true' or $default==1){
				$this->setDefault($name, true);
			}else{
				$this->setDefault($name, false);
			}
		}else{
			if($default!=''){
				$default = Util::processDefaultExpression($default, $att['type'], $dateformat);
	    		$this->setDefault($fieldName, $default);
	    		
				if($default=='currentDate'){
				 	$this->setDefault($name, date("d/m/Y"));
				}elseif($default=='currentHour'){
					$this->setDefault($name, date("H:i"));		
			 	}elseif($default=='linkedUser'){

			    }elseif($default=='firstDayOfMonth'){
			    	$year = date('Y');
			    	$month = date('m');
			    	$day = 1;
			    	$this->setDefault($name, date("d/m/Y",mktime(0,0,0,$month,$day,$year)));
			    }else{
					$this->setDefault($name, $default);
			   }			
			}
		}	*/
  	}	
  }
  
  
  public function configure_old()
  {
  	 parent::configure();
	 $i18N = sfContext::getInstance()->getI18N();
  	 $this->widgetSchema['dateFrom'] = new sfWidgetFormDateJS();
  	 $this->widgetSchema['dateUntil'] = new sfWidgetFormDateJS(); 
	
  	 $attributes = array();
  	 $attributes['class'] = 'date';
	 $attributes['tmt:datepattern'] = "DD/MM/YYYY";
     $attributes['tmt:message'] = $i18N->__("Incorrect date format"); 
  	 $attributes['class'] = $attributes['class'].' required';
  	 $attributes['class'] = $attributes['class'].' form';
	 $attributes['tmt:required'] = "true";
	 $attributes['tmt:errorclass']=  "invalid";
	 
	 
	 $this->widgetSchema['dateFrom']->setAttributes($attributes);
	 $this->widgetSchema['dateUntil']->setAttributes($attributes);
	 $day = 1;
	 $month = date('m');
	 $year = date('Y');
	 $firstDay=mktime(0,0,0,$month,$day,$year);
	 $this->widgetSchema['dateFrom']->setDefault(date('d/m/Y',$firstDay));
	 $lastDay = strtotime(date('d-m-Y',$firstDay) . ' + 1 month');
	 $lastDay = strtotime(date('d-m-Y',$lastDay) . ' - 1 day');
	 $this->widgetSchema['dateUntil']->setDefault(date('d/m/Y',$lastDay));
	 $attributes = array();
	 $attributes['class'] = 'combobox';
	 $attributes['class'] = $attributes['class'].' form';
	 
	 $objDef = new objectDef('dossier');
	 
	 $utilisateur = Doctrine::getTable('personnes')->findOneByNni(sfContext::getInstance()->getUser()->getAttribute('nni'));
	 $this->setWidgetsLocal($utilisateur->unitemanagerial_id, $utilisateur->site->departements->plaque->id , $utilisateur->site->departements->id, $utilisateur->site->id);	 
	 
	 $fields = array('categorie_id','type_id','domaine','um','plaqueClient','departementClient','siteClient'); 
	 foreach($fields as $fieldName){
		 $att = $objDef->attributes[$fieldName];
		 if( $att['ajaxFunction']!='') $attributes['onChange'] = $att['ajaxFunction'];
		 if($att['type']=='BO'){
		 	$fieldName2 = $fieldName.'_id';
		 }else{
		 	$fieldName2 = $fieldName;
		 }
		 //echo $fieldName2;
		 $this->widgetSchema[$fieldName2]->setAttributes($attributes);
	 }

	 $this->offsetGet('typePere')->getWidget()->setAttributes(array(
			'onchange'=>'refreshCandidatesType()',
			'class'=> "combobox form",	));
		 
	 $this->widgetSchema->setHelps(array()); //je vide les helps, car ici elles sont inutiles
	 
	 //$this->widgetSchema['dateUntil']->setLabel($i18N->__(''));
	 //$this->widgetSchema['dateUntil']->setLabel($i18N->__(''));
  }
  
  public function configParams(){
  	
  }
  public function hideWidget($fieldName){
   		$this->widgetSchema[$fieldName] = new sfWidgetFormInputHidden();
   		$this->widgetSchema[$fieldName]->setLabel(' ');
   		
  }  
  
}
















